package cn.wfu.rlrjds.web.repairman.contorller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.wfu.rlrjds.app.bean.RepaireSearch;
import cn.wfu.rlrjds.app.bean.RepairmanSearch;
import cn.wfu.rlrjds.app.bean.UserSearch;
import cn.wfu.rlrjds.app.repairman.domain.Repaireman;
import cn.wfu.rlrjds.app.repairman.service.IRepairmanService;
import cn.wfu.rlrjds.app.repairman.service.ipml.RepairmanException;
import cn.wfu.rlrjds.app.user.service.ipml.UserEcxeption;

@Controller(value = "webRepairmanContorller")
@RequestMapping(value = "/web/repairman")
public class RepairmanContorller {
	@Autowired
	private IRepairmanService repairmanService;

	@RequestMapping(value = "/findRepairmanById")
	public String findRepairmanById(HttpServletRequest request) {
		String id = request.getParameter("id");
		try {
			repairmanService.findRepairmanById(id);
			return "false";
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "true";
		}
	}

	/**
	 * 显示维修员
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", produces = "text/html;charset=UTF-8")
	public String list(HttpServletRequest request,
			RepairmanSearch repairmanSearch) {
		String id = repairmanSearch.getId();
		String nickName = repairmanSearch.getNickName();
		String phone = repairmanSearch.getPhone();
		RepairmanSearch search = new RepairmanSearch();
		if (StringUtils.isNotBlank(id)) {
			search.setId("%" + id + "%");
		}
		if (StringUtils.isNotBlank(nickName)) {
			search.setNickName("%" + nickName + "%");
		}
		if (StringUtils.isNotBlank(phone)) {
			search.setPhone("%" + phone + "%");
		}
		List<Repaireman> repairmans = this.repairmanService.list(search);
		request.setAttribute("repairmans", repairmans);
		return "/repairman/list";
	}

	/**
	 * 维修员注册
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/regist")
	public String regist(HttpServletRequest request, Repaireman repairman) {
		try {
			this.repairmanService.saveRepairman(repairman);
			return "redirect:/web" + this.list(request, new RepairmanSearch());
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("repairman", repairman);
			return add(request);
		}
	}

	/**
	 * 删除维修员
	 * 
	 * @param request
	 * @param repairman
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(HttpServletRequest request, Repaireman repairman) {
		String[] ids = request.getParameterValues("ids");
		try {
			this.repairmanService.deleteRepairman(ids);
			return list(request, new RepairmanSearch());
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "msg";
		}
	}

	/**
	 * 修改管理员的密码
	 * 
	 * @param request
	 * @param repairman
	 * @return
	 */
	@RequestMapping(value = "/updatePassword")
	public String updatePassword(HttpServletRequest request, Repaireman form) {
		String confirm_password = request.getParameter("confirm_password");
		if (!form.getPassword().equals(confirm_password)) {
			throw new RuntimeException("两次输入的密码不正确");
		}
		try {
			this.repairmanService.updatePasswordNoValidate(form);
			return list(request, new RepairmanSearch());
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "msg";
		}
	}

	/**
	 * 修改管理员的信息
	 * 
	 * @param request
	 * @param repairman
	 * @return
	 */
	@RequestMapping(value = "/updateInfo")
	public String updateInfo(HttpServletRequest request, Repaireman form) {
		try {
			this.repairmanService.updateRepairemanInfo(form, null);
			return "redirect:/web" + this.list(request, new RepairmanSearch());
		} catch (RepairmanException e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("repairman", form);
			return add(request);
		}
	}

	/**
	 * 转向添加视图
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add(HttpServletRequest request) {
		return "/repairman/add";
	}

	/**
	 * 跳转到修改密码页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editPassword")
	public String editPassword(HttpServletRequest request) {
		request.setAttribute("id", request.getParameter("id"));
		return "/repairman/edit_password";
	}

	/**
	 * 跳转到修改维修员信息页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editInfo")
	public String editInfo(HttpServletRequest request) {
		String id = request.getParameter("id");
		RepairmanSearch repairemanSearch = new RepairmanSearch();
		repairemanSearch.setId(id);
		List<Repaireman> repairemans = this.repairmanService
				.list(repairemanSearch);
		if (repairemans != null && !repairemans.isEmpty()) {
			request.setAttribute("repaireman", repairemans.get(0));
			return "/repairman/edit_info";
		}
		return null;
	}
}
